package com.hyzh.hj212.controller;

import com.coffee.core.CoffeeResponse;
import com.coffee.core.CoffeeResponseEnum;
import com.hyzh.hj212.dmo.AirEnvVO;
import com.hyzh.hj212.dmo.StatsDataVO;
import com.hyzh.hj212.enterprise.FieldCodeEnum;
import com.hyzh.hj212.enterprise.TypeToCommandEnum;
import com.hyzh.hj212.service.LatteAirService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.Map;

/**
 * 数据从算法组
 * 算法组 有数据 就会调用该接口
 *
 * @author songkui
 * @since 2024/3/15 23:15
 */
@RestController
@RequestMapping("/air")
@Slf4j
@AllArgsConstructor
public class AirController {

    private final LatteAirService latteAirService;
    //header : park : 园区编码;

    // 实时数据  item: 传对应参数项；
    @GetMapping("/rtd")
    public CoffeeResponse<StatsDataVO> rtd(@RequestHeader("park") String park, @RequestParam("item") String item) {

        FieldCodeEnum fieldCodeEnum = FieldCodeEnum.ofCode(item);
        if (null == fieldCodeEnum) {
            log.error("Air-Rtd >> item {} Invalid", item);
            return CoffeeResponse.response(CoffeeResponseEnum.ParamInvalid);
        }
        return CoffeeResponse.responseSuccess(latteAirService.rtdData(fieldCodeEnum));
    }


    //按时间查询  ;  type: M: 分钟;  H: 时；  D：天
    @GetMapping("/environment")
    public CoffeeResponse<Map<String, List<AirEnvVO>>> mapEnvironment(@RequestHeader("park") String park,
                                                                      @RequestParam("type") String type,
                                                                      @RequestParam(name = "start") String start,
                                                                      @RequestParam(name = "end") String end) {

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime startDateTime = LocalDateTime.parse(start, formatter);
        LocalDateTime endDateTime = LocalDateTime.parse(end, formatter);
        long days = startDateTime.until(endDateTime, ChronoUnit.DAYS);
        if (days > 15) {
            return CoffeeResponse.responseMessage(CoffeeResponseEnum.ParamInvalid.code(), "时间间隔不能大于15天");
        }

        TypeToCommandEnum typeToCommandEnum = TypeToCommandEnum.ofCode(type);
        if (null == typeToCommandEnum) {
            log.error("Air-Rtd >> typ {} Invalid must：[M H D]", type);
            return CoffeeResponse.response(CoffeeResponseEnum.ParamInvalid);
        }

        Map<String, List<AirEnvVO>> data = latteAirService.mapEnvironment(typeToCommandEnum, startDateTime, endDateTime);
        return CoffeeResponse.responseSuccess(data);
    }

    //统计  item: 传对应参数项；
    @GetMapping("/stats")
    public CoffeeResponse<StatsDataVO> stats(@RequestHeader("park") String park,
                                             @RequestParam("item") String item,
                                             @RequestParam(name = "start", required = false) String start,
                                             @RequestParam(name = "end", required = false) String end) {

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime startDateTime = LocalDateTime.parse(start, formatter);
        LocalDateTime endDateTime = LocalDateTime.parse(end, formatter);
        long days = startDateTime.until(endDateTime, ChronoUnit.DAYS);
        if (days > 15) {
            return CoffeeResponse.responseMessage(CoffeeResponseEnum.ParamInvalid.code(), "时间间隔不能大于15天");
        }

        FieldCodeEnum fieldCodeEnum = FieldCodeEnum.ofCode(item);
        if (null == fieldCodeEnum) {
            log.error("Air-Rtd >> item {} Invalid", item);
            return CoffeeResponse.response(CoffeeResponseEnum.ParamInvalid);
        }
        StatsDataVO data = latteAirService.statsData(fieldCodeEnum, startDateTime, endDateTime);
        return CoffeeResponse.responseSuccess(data);

    }



}
